package com.usian.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.usian.feign.UserFeign;
import com.usian.handler.GlobalBlockHandler;
import com.usian.handler.GlobalFallback;
import com.usian.pojo.User;
import com.usian.query.UserDto;
import com.usian.utils.AppHttpCodeEnum;
import com.usian.utils.PageResult;
import com.usian.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.List;

@RestController
public class UserController {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @Autowired
    private UserFeign userFeign;

    @RequestMapping("/conList")
    public List<User> conList(){

        //consumer 到 nacos 找到 provider 的实例接口
        /*ServiceInstance instance = loadBalancerClient.choose("provider");

        String url = "http://"+instance.getHost()+":"+instance.getPort()+"/list";

        List<User> userList = restTemplate.getForObject(url, List.class);*/

        List<User> userList = userFeign.list();
        return userList;
    }

    @GetMapping("/findById/{id}/{name}")
    @SentinelResource(value = "findById",
                        blockHandler = "test",
                        blockHandlerClass = GlobalBlockHandler.class,
                        fallbackClass = GlobalFallback.class,
                        fallback = "fallBackTest")
    public User findById(@PathVariable Integer id,@PathVariable String name){
        int a =10/0;
        return userFeign.findById(id);
    }


    @GetMapping("/findByIds")
    public User findByIds(@RequestParam Integer id, @RequestParam String name){
        return userFeign.findByIds(id,name);
    }


    /**
     * 分页查询用户列表
     *
     * @param dto
     * @return
     */
    @RequestMapping("/findPage")
    public ResponseResult<User> findPage(@RequestBody UserDto dto, HttpServletRequest request){


        PageResult<User> page =new PageResult<>();
        try {
            //request.setCharacterEncoding("UTF-8");
            String id = request.getHeader("id");
            String username1 = request.getHeader("username");
            String username = URLDecoder.decode(username1,"UTF-8");
            page = userFeign.findPage(dto);


        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return ResponseResult.okResult(page);
    }

    /**
     * 保存用户
     *
     * @param user
     * @return
     */
    @PostMapping("/saveUser")
    public ResponseResult<User> saveUser(@RequestBody User user){
        boolean b = userFeign.saveUser(user);
        if(b){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     */
    @GetMapping("/deleteById")
    public ResponseResult<User> deleteById(@RequestParam Integer id){
        boolean result = userFeign.deleteById(id);
        if(result){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }else{
            return ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
        }

    }


    /**
     * 登录
     *
     * @param user
     * @return
     */
    @RequestMapping("/login")
    public ResponseResult<User> login(@RequestBody User user){

        User user1 = userFeign.login(user);
        if(user1==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
        }else{
            return ResponseResult.okResult(user1);
        }
    }

}
